{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e432191d-11a8-453f-9cbc-4d297e29bdfe",
   "metadata": {},
   "source": [
    "## 1、python的基础"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "70c1dc35-d681-4120-83af-88d42177f255",
   "metadata": {},
   "source": [
    "### 1.1、Python的基础输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a73fb8ee-65e3-4cda-8d7a-4b1685ce55de",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\n"
     ]
    }
   ],
   "source": [
    "print(\"hello world\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4de85c4d-9264-48b2-aaf1-e715d41c355a",
   "metadata": {},
   "source": [
    "### 1.2、变量的声明"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "025cd46b-7924-4977-b556-afabdd3f1229",
   "metadata": {},
   "source": [
    "python中的变量是弱类型的，不用强制声明数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "27d5bb69-7bac-43cb-8282-e3edec237652",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'int'>\n",
      "<class 'str'> hello world\n",
      "<class 'float'> 12.3\n",
      "\n",
      "多行注释，其实是多行的字符串，\n",
      "可以换行，如果没有变量来获取这个值，它就无效，相当于多行注释\n",
      "\n"
     ]
    }
   ],
   "source": [
    "a = 10\n",
    "# a目前是整数\n",
    "print(type(a))\n",
    "# a变成了string\n",
    "a = \"hello world\"\n",
    "print(type(a),a)\n",
    "# a变成了float\n",
    "a = 12.3\n",
    "print(type(a),a)\n",
    "'''\n",
    "没有用变量来声明，就意味着不会创建任何内存，这也就相当于多行注释\n",
    "'''\n",
    "\n",
    "b = '''\n",
    "多行注释，其实是多行的字符串，\n",
    "可以换行，如果没有变量来获取这个值，它就无效，相当于多行注释\n",
    "'''\n",
    "print(b)#因为我使用b来接收了三个单引号的字符串，此时这个换行的字符串就被b所接收了"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dc686b67-b3af-4f4d-9afe-64b7e9788e9f",
   "metadata": {},
   "source": [
    "### 1.3、整数类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "aa22781f-4041-4979-a6e6-6b0305d4fc6f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1475368872528 <class 'int'> 10\n",
      "1475368872528 <class 'int'> 10\n",
      "1475368872848 <class 'int'> 20 10\n",
      "1475368873168 30\n",
      "1475368872528 10\n"
     ]
    }
   ],
   "source": [
    "a = 10\n",
    "print(id(a),type(a),a)\n",
    "b = 10 # b=a\n",
    "print(id(b),type(b),b)\n",
    "b = 20\n",
    "print(id(b),type(b),b,a)\n",
    "a = 30\n",
    "print(id(a),a) #a的id会发生变化\n",
    "c = 10\n",
    "print(id(c),c) #c的内存是原来10的那个内存，"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9b2d8b9b-df9a-4f05-881e-06d3460b4483",
   "metadata": {},
   "source": [
    "出现以上结果的原因是python有一个内存驻留的机制，它会自动把一些常见的字符串和0-1024之间的整数存储在单独的内存里面，如果使用到，就直接调用这块内存，这样就不用新分配内存空间了!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5ec23ab3-734c-46df-89dd-984a1b0fbd4e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1475446198448 1999\n",
      "1475446198960 2000\n",
      "1475446198352 1999\n"
     ]
    }
   ],
   "source": [
    "a = 1999\n",
    "print(id(a),a)\n",
    "a = 2000\n",
    "print(id(a),a)\n",
    "a = 1999\n",
    "print(id(a),a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df93214f-e5d6-42a5-aa12-d6037f793ebc",
   "metadata": {},
   "source": [
    "### 1.4、小数类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ab4ea1ab-9599-40ac-bc55-3c979a15f686",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1475445804848 <class 'float'> 12.3\n",
      "1475445804944 <class 'float'> 24.3\n"
     ]
    }
   ],
   "source": [
    "a = 12.3\n",
    "print(id(a),type(a),a)\n",
    "b = 12\n",
    "c = a+b#浮点数和整数运算，会自动将整数转换为浮点数\n",
    "print(id(c),type(c),c)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de1dfae4-bf0a-47e5-9e91-76e1be60e04f",
   "metadata": {},
   "source": [
    "### 1.5、字符串类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "96f78e34-0a9f-4da0-a756-ea31cf2088bc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "23 1475446183152 <class 'str'>\n",
      "1023\n",
      "33\n"
     ]
    }
   ],
   "source": [
    "s1 = \"23\"\n",
    "s1 = '23'\n",
    "print(s1,id(s1),type(s1))\n",
    "a = 10\n",
    "#s2 = a+s1 #python中不能用整数来加字符串，可以加浮点，是因为可以进行数字之间可以进行运算（其实是因为int这个类重写了add这个方法）\n",
    "s2 = str(a)+s1 #强制类型转换\n",
    "print(s2)\n",
    "c = a+int(s1)#将str强制转换为int\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "2c5c3589-b03b-4547-8a33-4cc376099672",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1475446185392 1475446185392\n"
     ]
    }
   ],
   "source": [
    "s1 = \"aabbccdd\"\n",
    "s2 = \"aabbccdd\"\n",
    "print(id(s1),id(s2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e85b272c-735c-49bc-bc87-3afb237d39eb",
   "metadata": {},
   "source": [
    "### 1.6、bool类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "af07bcb0-90b1-49d1-ba92-7a29394b5ace",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "140716139866216 140716139866248 140716139866216 True False True\n"
     ]
    }
   ],
   "source": [
    "a = True #True和False的第一个字母是大写\n",
    "b = False\n",
    "c = True\n",
    "print(id(a),id(b),id(c),a,b,c)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35b60d46-ce65-48ab-ad3d-f73f43f2f8ad",
   "metadata": {},
   "source": [
    "### 1.7、输入和输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "b9cf371c-d88d-402c-aba4-f4451459063f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入a的值 1\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 1475369495664 <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "a = input(\"请输入a的值\")\n",
    "print(a,id(a),type(a)) #输入之后是字符串类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "28c50f79-809f-453a-9673-1f2370a5f140",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入姓名: 1\n",
      "请输入年龄: 1\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我叫1，我的年龄是:1，再过一年，我的年龄是2\n"
     ]
    }
   ],
   "source": [
    "name = input(\"请输入姓名:\")\n",
    "age = int(input(\"请输入年龄:\"))\n",
    "print(\"我叫\"+name+\"，我的年龄是:\"+str(age)+\"，再过一年，我的年龄是\"+str(age+1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "acada0f4-5133-4fd6-95d9-a0946e55e4ef",
   "metadata": {},
   "source": [
    "有三种方式可以进行输出的控制\n",
    "1、通配符\n",
    "2、占位符\n",
    "3、f通配符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "72675efe-373b-4bc1-8087-165c905787fc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入姓名: 1\n",
      "请输入年龄: 1\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名是:1,年龄是1,一年之后是2\n",
      "姓名是1,年龄是1,一年之后是2\n",
      "姓名是1,年龄是1,一年之后是2\n"
     ]
    }
   ],
   "source": [
    "name = input(\"请输入姓名:\")\n",
    "age = int(input(\"请输入年龄:\"))\n",
    "print(\"姓名是:%s,年龄是%d,一年之后是%d\"%(name,age,age+1)) #基于C语言的通配符方式\n",
    "print(\"姓名是{},年龄是{},一年之后是{}\".format(name,age,age+1))#基于占位符\n",
    "print(f\"姓名是{name},年龄是{age},一年之后是{age+1}\") #最常见的使用方式"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4b6ae9c2-5843-416e-a3f8-140c0aedc63f",
   "metadata": {},
   "source": [
    "## 2.运算符"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8e4136a8-f543-44bb-993a-659c32dc36dd",
   "metadata": {},
   "source": [
    "### 2.1、运算符基础"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "ccb6e17a-9960-40e7-a78f-36737fcd6390",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.5\n",
      "1\n",
      "2\n",
      "12\n",
      "8\n",
      "$$$$$$$$$$\n"
     ]
    }
   ],
   "source": [
    "print(1/2)#会自动转换为float\n",
    "print(3//2)#除完之后取整\n",
    "print(10%4)#取余数\n",
    "print(3*4)#12\n",
    "print(2**3)#表示2的3次方\n",
    "print(\"$\"*10)#输出10遍$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "edbf7c56-21a4-46df-b74f-76234bf4ab42",
   "metadata": {},
   "source": [
    "### 2.2、赋值运算符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "43ceecb7-d54c-45bd-9703-2f9d195b0408",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10 10 10\n"
     ]
    }
   ],
   "source": [
    "a = b = c = 10\n",
    "print(a,b,c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "c9651c6c-802e-496e-bed6-89f348ac1dea",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12 22 33\n"
     ]
    }
   ],
   "source": [
    "a,b,c = 12,22,33\n",
    "print(a,b,c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "696a5e96-49a8-4404-88fe-82dfa918f8d1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20 10\n"
     ]
    }
   ],
   "source": [
    "a,b = 10,20\n",
    "a,b = b,a #数值交换\n",
    "print(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "6818aa1e-e742-49e5-b481-5ccb5aa8255e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20\n"
     ]
    }
   ],
   "source": [
    "a = 10\n",
    "a+=10\n",
    "print(a)\n",
    "#a++ python没有++和--"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77b9a37d-e291-4e25-a13d-fe3feb9d8f19",
   "metadata": {},
   "source": [
    "### 2.3、比较运算符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "dd913980-447c-43b0-a505-ada85824e221",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n",
      "1475446199088 1022\n",
      "1475446199664 1022\n",
      "True\n",
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "a = 1099\n",
    "b = 1099\n",
    "print(a is b) #比较id 1099没有内存驻留，所以id不一样，但是如果是10，有内存驻留，id一样\n",
    "print(a==b)#比较val\n",
    "c = 1022\n",
    "print(id(c),c)\n",
    "c+=0\n",
    "print(id(c),c) #id不一样，因为没有内存驻留\n",
    "print(c>=1022)\n",
    "print(c>=10 & b==1099) #python没有两个&&，一个&是可以的，但是再做位运算\n",
    "print(c>=0 and b==1099) #and是与，or是或\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91a36bef-4120-486d-bf40-3c1691c0f343",
   "metadata": {},
   "source": [
    "### 2.4、对象的布尔值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "b31cfd09-e51e-48c2-9438-f22b0d5c338f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "print(bool(False))\n",
    "print(bool(0))\n",
    "print(bool(0.0))\n",
    "print(bool(''))\n",
    "print(bool(None))\n",
    "print(bool(\"\")) #空字符\n",
    "print(bool([])) #空列表\n",
    "print(bool(())) #空元组\n",
    "print(bool({})) #空字典\n",
    "print(bool(dict())) #空字典\n",
    "print(bool(set())) #空集合\n",
    "print(bool(-1)) #-1的bool值是true"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de5684a7-8770-4852-bd81-5f905f7c6119",
   "metadata": {},
   "source": [
    "## 3.语句结构\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d992539f-7b7d-4360-8a25-82670f089bc0",
   "metadata": {},
   "source": [
    "### 3.1、顺序结构"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "3d754f61-85e2-45fa-a1ef-b026d0a6a9a9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n"
     ]
    }
   ],
   "source": [
    "a = 10\n",
    "print(a)\n",
    "a+=20"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ddca30a-2ae4-4ac5-b235-aacc866380cc",
   "metadata": {},
   "source": [
    "### 3.2、选择结构"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "bb4f5de4-51a0-480f-ac62-ddba45fc55bd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a>=10\n",
      "30\n",
      "20\n"
     ]
    }
   ],
   "source": [
    "a = 10\n",
    "if a>=10:\n",
    "    print(\"a>=10\")\n",
    "    print(30)\n",
    "print(20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "3a2ed338-a716-44ad-b027-90083c336f6e",
   "metadata": {},
   "outputs": [],
   "source": [
    "if a==10:\n",
    "    #不能有空语句块，否则编译就报错了!\n",
    "    pass #如果有个空语句块，可以加个pass表示什么都不做"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "29350dc3-aabb-4abe-8afd-12f0072d9368",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入成绩: 22\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "准备补考\n"
     ]
    }
   ],
   "source": [
    "ach = int(input(\"请输入成绩:\"))\n",
    "if ach>=90:\n",
    "    print(\"A\")\n",
    "elif a>=80:\n",
    "    print(\"B\")\n",
    "elif a>=70:\n",
    "    print(\"C\")\n",
    "elif a>=60:\n",
    "    print(\"不多不少!\")\n",
    "else:\n",
    "    print(\"准备补考\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53b926fd-8067-4c41-847f-6c4f6e84f39e",
   "metadata": {},
   "source": [
    "#### 3.2.2、条件表达式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "d8dea697-14b7-4ba9-8c6c-78325f82a584",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20<30\n"
     ]
    }
   ],
   "source": [
    "a,b = 20,30\n",
    "print(f\"{a}>{b}\") if a>b else print(f\"{a}<{b}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0dc977ce-f4d8-4989-ab2a-0fa7ced57064",
   "metadata": {},
   "source": [
    "### 3.3、循环结构"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b0d9844f-bd00-47d8-af04-42a063d2a219",
   "metadata": {},
   "source": [
    "#### 3.3.1、for循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "cb6e1d26-c2e4-49f1-84f1-f5c77b375fc5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'range'> 1475463064816 range(0, 10) 48\n",
      "48\n",
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "----------------------------------------------------------------------------------------------------\n",
      "100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "r1 = range(10) ##会产生0-9的一个迭代器，相当于 range(0,10)\n",
    "#原则式包头不包尾\n",
    "print(type(r1),id(r1),r1,sys.getsizeof(r1))\n",
    "r2 = range(100,200)\n",
    "#迭代器的好处就是用到才分配内存\n",
    "print(sys.getsizeof(r2))\n",
    "\n",
    "for i in r1:\n",
    "    print(i)\n",
    "print(\"-\"*100)\n",
    "for i in r2:\n",
    "    print(i,end=\",\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d1111de4-dd6e-4168-932e-b6871e00d56e",
   "metadata": {},
   "source": [
    "#### 3.3.2、while循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "376b7e86-69ed-4f98-bbac-5c5f7374d8ed",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "i = 1\n",
    "while i<=10:\n",
    "    if i==5:\n",
    "        i+=1\n",
    "        # continue\n",
    "        break\n",
    "    print(i)\n",
    "    i+=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "fe7f5df2-6259-4b27-95a3-da6c4df3a30c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1683\n"
     ]
    }
   ],
   "source": [
    "#输出1-100之间能够被3整除的数之和\n",
    "sum = 0\n",
    "for i in range(1,101):\n",
    "    if i%3==0:\n",
    "        sum+=i\n",
    "print(sum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "b4bab397-7127-4b8c-bc64-952b6ffd67ee",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1*1=1\t\n",
      "1*2=2\t2*2=4\t\n",
      "1*3=3\t2*3=6\t3*3=9\t\n",
      "1*4=4\t2*4=8\t3*4=12\t4*4=16\t\n",
      "1*5=5\t2*5=10\t3*5=15\t4*5=20\t5*5=25\t\n",
      "1*6=6\t2*6=12\t3*6=18\t4*6=24\t5*6=30\t6*6=36\t\n",
      "1*7=7\t2*7=14\t3*7=21\t4*7=28\t5*7=35\t6*7=42\t7*7=49\t\n",
      "1*8=8\t2*8=16\t3*8=24\t4*8=32\t5*8=40\t6*8=48\t7*8=56\t8*8=64\t\n",
      "1*9=9\t2*9=18\t3*9=27\t4*9=36\t5*9=45\t6*9=54\t7*9=63\t8*9=72\t9*9=81\t\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,10):\n",
    "    for j in range(1,i+1):\n",
    "        print(f\"{j}*{i}={i*j}\",end=\"\\t\")\n",
    "    print()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "545afc96-7487-41d7-86a6-c36a99bda4fb",
   "metadata": {},
   "source": [
    "### 3.3.3、循环中的else\n",
    "在python中循环结构可以写else"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "2de85a80-bd9a-461c-b898-fad87a1de341",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "all end\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,10):\n",
    "    if i%5==0:\n",
    "        # continue\n",
    "        break\n",
    "    print(i)\n",
    "else: #循环中的else会在循环正常结束之和执行，如果循环是break退出的，就不会执行else\n",
    "    print(\"end for\") \n",
    "print(\"all end\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d8631a8-1a90-4ecb-af35-72fb17af552b",
   "metadata": {},
   "source": [
    "## 4.集合操作\n",
    "### 4.1、列表操作\n",
    "#### 4.1.2、列表的基础"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "c2850f37-79bb-408a-bc72-bdfa853ac4e7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[] <class 'list'> 1475475042560\n",
      "[1, 2]\n",
      "1\n",
      "2\n",
      "----------\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "lst = list()\n",
    "print(lst,type(lst),id(lst))\n",
    "lst.append(1)\n",
    "lst.append(2)\n",
    "print(lst)\n",
    "for i in lst:\n",
    "    print(i)\n",
    "#获取集合中的元素和java的数组一样\n",
    "print(\"-\"*10)\n",
    "print(lst[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "40863031-89e3-4b43-b32c-f0702bd8bf86",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'range'> <class 'list'> 48 848\n",
      "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "r1 = range(1,100) #range是个迭代器，占用空间固定\n",
    "lst = list(range(1,100))#list会直接分配空间\n",
    "print(type(r1),type(lst),sys.getsizeof(r1),sys.getsizeof(lst))\n",
    "for i in lst:\n",
    "    print(i,end=',')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "08fe12e5-a015-4b90-a247-c3ae0e0fbe94",
   "metadata": {},
   "source": [
    "#### 4.1.3、列表生成式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "68dee340-4a23-4dc7-bc29-d1980f5e3f33",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4] [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]\n",
      "[1, 4, 9, 16, 25, 36, 49, 64, 81]\n",
      "[4, 16, 36, 64]\n",
      "[0, 1, 2, 9, 4, 25, 6, 49, 8, 81]\n"
     ]
    }
   ],
   "source": [
    "lst1 = list([1,2,3,4])\n",
    "lst2 = list(range(10,20))\n",
    "print(lst1,lst2)\n",
    "#列表生成式，生成式的结构: 表达式   循环条件\n",
    "lst3 = [x**2 for x in range(1,10)]\n",
    "print(lst3)\n",
    "#基于筛选条件的列表生成式\n",
    "lst3 = [x**2 for x in range(1,10) if x%2==0]\n",
    "print(lst3)\n",
    "#如果要进行更复杂的控制，可以使用条件表达式\n",
    "lst4 = [x if x%2==0 else x**2 for x in range(10)]\n",
    "print(lst4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f03ac160-f9d3-4b85-9a9f-c252a0e41322",
   "metadata": {},
   "source": [
    "#### 4.1.3、列表的切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "3a590a75-0c32-4b56-8e44-fd268b662c32",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]\n",
      "[10, 12, 14, 16, 18]\n",
      "[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]\n",
      "[10, 12]\n",
      "[11, 12, 13, 14]\n",
      "[19, 18, 17, 16, 15, 14, 13, 12, 11, 10]\n",
      "[18, 16, 14, 12]\n",
      "[18, 16, 14, 12, 10]\n"
     ]
    }
   ],
   "source": [
    "lst = list(range(10,20))\n",
    "print(lst)\n",
    "#切片的结构 lst[start:end:step] #如果不写start表示从0开始，如果不写end，表示到最后，如果不写step表示为1\n",
    "#切片完成之后得到的是一个新的列表\n",
    "print(lst[::2])\n",
    "print(lst)\n",
    "print(lst[:4:2]) #同样是包头不包尾\n",
    "print(lst[1:5:])\n",
    "#step可以是-1，表示倒着走\n",
    "print(lst[::-1])\n",
    "print(lst[8:0:-2])\n",
    "print(lst[-2::-2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "27eb0c1f-cf25-4529-a990-52b827fcf138",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
      "[0, 2, 4, 6, 8, 10]\n",
      "[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]\n",
      "[0, 3, 6, 9]\n",
      "[9, 7, 5, 3, 1]\n",
      "[9, 7, 5, 3, 1]\n"
     ]
    }
   ],
   "source": [
    "lst = list(range(0,11))\n",
    "print(lst)\n",
    "#取出偶数下标的元素\n",
    "print(lst[::2])\n",
    "#反转字符串\n",
    "print(lst[::-1])\n",
    "#隔两个取一位\n",
    "print(lst[::3])\n",
    "#倒序偶数位\n",
    "print(lst[-2::-2])\n",
    "#列表中提取奇数位元素并且反向输出\n",
    "print(lst[1::2][::-1])\n",
    "#奇数和偶数交换"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3b4305f-d588-4962-b685-fa2b53b9ccd8",
   "metadata": {},
   "source": [
    "#### 4.1.4、列表的基本操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8537edca-0106-438c-a931-04c1aaca75de",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3130100424768 [1, 2, 3, 4, 5, 6, 7, 8, 9] <class 'list'>\n",
      "1\n",
      "[1, 2, 3, 4, 5, 6, 7, 8, 9, 999] 3130100424768\n",
      "3129994800016 3129994799696\n"
     ]
    }
   ],
   "source": [
    "lst = list(range(1,10))\n",
    "print(id(lst),lst,type(lst))\n",
    "print(lst[0])\n",
    "lst.append(999)\n",
    "print(lst,id(lst))\n",
    "a = 10\n",
    "b = a\n",
    "a = 20\n",
    "print(id(a),id(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "617e5b37-8385-4783-a11b-fcc5aa0ed9dd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'd', 'e', 'f']"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst = ['a','b','c','d','e','f','g']\n",
    "lst.pop() #删除最后一个元素\n",
    "lst.pop(1) #删除第一个元素\n",
    "lst.remove('c')\n",
    "# lst.remove3('c')#会抛异常\n",
    "lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "3d0d37ef-9a0d-4c9f-a16e-48fe0cc8ae1d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 11, 22, 33, 223, 3, 4]\n",
      "3\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "lst = [1,2,3,4]\n",
    "lst.insert(2,223) #在第二个位置插入223\n",
    "lst\n",
    "#它是在第二个位置插入了一个数组\n",
    "# lst.insert(2,[11,22,33])\n",
    "lst\n",
    "#如果希望在某个位置插入相应的数组中的元素\n",
    "# lst[2::] = [11,22,33]\n",
    "lst[2:2] = [11,22,33] #在2的位置替换为11,22,33，变相等于插入\n",
    "print(lst)\n",
    "print(lst.index(22))#返回22这个元素的下标\n",
    "# print(lst.index(323))#没有找到会抛出异常\n",
    "print(lst[-2])#倒着找第二位"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "4cefc450-62bf-4a07-880d-ebb1eab34fb5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3130108987328\n",
      "[51, 23, 23, 21, 4, 2, 1] 3130108987328\n",
      "[1, 1, 4, 12, 42, 42, 51] 3130107218624 3130108574656\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "lst = [23,4,1,23,21,51,2]\n",
    "# lst.sort()\n",
    "print(id(lst))\n",
    "lst.sort(reverse=True)#倒序\n",
    "print(lst,id(lst))#是在列表本身上面进行排序\n",
    "lst2 = [42,1,51,1,4,42,12]\n",
    "lst2_s = sorted(lst2) #sorted会排序之后生成一个新的列表\n",
    "print(lst2_s,id(lst2_s),id(lst2))\n",
    "print(22 in lst)#判断22有没有在lst中"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e8ea9647-f020-43d2-ba8b-a7498314e927",
   "metadata": {},
   "source": [
    "### 4.2、字典操作\n",
    "#### 4.2.1、字典基础"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "3df5c770-f11a-41f0-902d-49d11a6de01e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 10, 'b': 30, 'd': 50}"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = {\"a\":10,\"b\":20}\n",
    "d[\"a\"]\n",
    "d[\"d\"] = 50 #会插入一个d值为50\n",
    "d\n",
    "d[\"b\"] = 30 #如果存在不会插入，会修改\n",
    "d"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0ae2954e-58f2-4275-b64a-1a0653611914",
   "metadata": {},
   "source": [
    "#### 4.2.2、字典的常用操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "1d5975c3-53ea-4a87-a39a-d4329da20cfb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n",
      "10\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'b': 20, 'c': 30}"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = {\"a\":10,\"b\":20,\"c\":30}\n",
    "print(\"a\" in d) #判断a是不是字典的键\n",
    "print(\"aa\" in d)#不存在的话会输出False\n",
    "x = d.pop(\"a\")#删除一个元素并且返回值\n",
    "print(x) #x=10\n",
    "d\n",
    "# d.pop()#字典的pop必须有删除的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "40104849-2e1a-47c0-8eee-3cd9494aa24b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "学号      姓名      年龄    \n",
      "1001      小明      22    \n",
      "1002      小王      21    \n",
      "1003      小赵      21    \n"
     ]
    }
   ],
   "source": [
    "stu = {\"no\":\"1001\",\"name\":\"小明\",\"age\":15}\n",
    "stu['name']\n",
    "stus = [\n",
    "    {\"no\":\"1001\",\"name\":\"小明\",\"age\":22},\n",
    "    {\"no\":\"1002\",\"name\":\"小王\",\"age\":21},\n",
    "    {\"no\":\"1003\",\"name\":\"小赵\",\"age\":21},\n",
    "]\n",
    "# for stu in stus:\n",
    "#     print(f\"no:{stu['no']},name:{stu['name']},age:{stu['age']}\")\n",
    "format_title = \"{:<8}{:<8}{:<6}\"\n",
    "format_data = \"{:<10}{:<8}{:<6}\"\n",
    "print(format_title.format(\"学号\",\"姓名\",\"年龄\"))\n",
    "for stu in stus:\n",
    "    print(format_data.format(stu['no'],stu['name'],stu['age']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "e9eaac95-dfce-4415-8a2f-9aa9538c736a",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       abc       bcd\n"
     ]
    }
   ],
   "source": [
    "s1 = \"abc\"\n",
    "s2 = \"bcd\"\n",
    "print(\"{:>10}{:>10}\".format(s1,s2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "85c0b553-8962-4f67-b4ca-5224571c399a",
   "metadata": {},
   "source": [
    "#### 4.2.3、字典的增删改查"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "cf4c861d-4612-4027-b2a2-db5db5ae0595",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_keys(['a', 'b'])\n",
      "a:10\n",
      "b:20\n",
      "dict_values([10, 20])\n",
      "{}\n",
      "33\n",
      "33\n"
     ]
    }
   ],
   "source": [
    "d = dict() #创建一个字典\n",
    "d = {\"a\":10,'b':20}\n",
    "print(d.keys())\n",
    "#遍历字典\n",
    "for key in d.keys():\n",
    "    print(f\"{key}:{d[key]}\")\n",
    "print(d.values())#获取所有的values\n",
    "d.clear() #清空字典\n",
    "print(d)\n",
    "d = {'a':10,'b':20,'c':30}\n",
    "d['a']\n",
    "d['d'] = 33\n",
    "# d['d']#如果没有这个元素会抛出异常\n",
    "print(d.get('d')) #获取字典的元素使用get方法，如果没有会输出None\n",
    "if d.get('d'):\n",
    "    print(d['d'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0afd7f55-b98d-4031-91c4-d27a8ce24017",
   "metadata": {},
   "source": [
    "#### 4.2.4、字典生成式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "8a290379-9781-408d-86ab-dc132507dfbc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a': 0, 'b': 22, 'c': 0, 'd': 0}\n"
     ]
    }
   ],
   "source": [
    "lst1 = ['a','b','c','d']\n",
    "lst2 = [11,22,33,55]\n",
    "lst3 = [12,3,4,3]\n",
    "#zip会把列表的元素添加成为元组\n",
    "# for t in zip(lst1,lst2,lst3):\n",
    "#     print(t)\n",
    "d = {k:(v if v%2==0 else 0) for k,v in zip(lst1,lst2)}\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cc670428-abfb-470f-9da6-ed499bde3948",
   "metadata": {},
   "source": [
    "### 4.3、元组\n",
    "元组类似于列表，区别就是元组是不可变类型，用（）来创建，方法少，效率特别高"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "3ddde80f-5934-40fc-af61-4f29738ba41d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1926603912576 <class 'tuple'> (1, 2, 3)\n",
      "<class 'int'>\n",
      "1926540256880 <class 'tuple'> (12,)\n",
      "(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99)\n"
     ]
    }
   ],
   "source": [
    "t = (1,2,3)\n",
    "print(id(t),type(t),t)\n",
    "lst = [1,2,3]\n",
    "lst.append(12)\n",
    "lst\n",
    "lst[0] = 12\n",
    "lst\n",
    "# t[0] = 11#会报错，元组不支持修改功能\n",
    "t1 = (12) #此时创建的是int类型\n",
    "print(type(t1))\n",
    "#如果希望创建一个元素的元组需要用下面方式\n",
    "t2 = (12,) #在一个元素之后加个,就是元组\n",
    "print(id(t2),type(t2),t2)\n",
    "\n",
    "#元组生成式\n",
    "t3 = tuple(a for a in range(100))\n",
    "print(t3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "3811e0c3-6688-442c-973f-5fc2a393fdf1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1926588509312 <class 'zip'> <zip object at 0x000001C0919FAC80>\n",
      "a 11 haha\n",
      "b 22 huhu\n",
      "c 33 hehe\n",
      "d 44 yeah\n"
     ]
    }
   ],
   "source": [
    "lst1 = ['a','b','c','d']\n",
    "lst2 = [11,22,33,44]\n",
    "lst3 = ['haha','huhu','hehe','yeah']\n",
    "\n",
    "tt = zip(lst1,lst2,lst3)\n",
    "print(id(tt),type(tt),tt)\n",
    "#zip是迭代器，迭代一次之后就会到最后的位置，不能继续遍历\n",
    "# for t in tt:\n",
    "#     print(t)\n",
    "for a,b,c in tt:\n",
    "    print(a,b,c)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b21ee48a-ae97-46fb-82ab-3e7725edfb20",
   "metadata": {},
   "source": [
    "### 4.4、集合\n",
    "集合相当于java中set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "3cba38e9-b15c-493f-b74e-5bfd3251f2f2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{10, 50, 20, 30} 1926593120064 <class 'set'>\n",
      "{40, 10, 20, 30} {40, 10, 20, 30} 1926598478752 1926598479200\n",
      "{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}\n"
     ]
    }
   ],
   "source": [
    "s = {10,30,50,50,20}\n",
    "print(s,id(s),type(s))\n",
    "s.add(1212)\n",
    "s\n",
    "s.remove(1212)\n",
    "s\n",
    "# s.remove(1212) #删除不存在的会报错\n",
    "s.discard(1212) #使用discard，删除不存在的不会报错\n",
    "s.discard(10)\n",
    "s\n",
    "s1 = {10,20,30,40}\n",
    "s2 = {20,10,30,40}\n",
    "print(s1,s2,id(s1),id(s2))\n",
    "\n",
    "#集合生成式 s3 = set(x for x in range(xx))\n",
    "s3 = {t for t in range(12)}\n",
    "print(s3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "52d55e80-16f4-49be-aecc-a5081430652b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "s1 = {10,20,30}\n",
    "s2 = {10,30,20,50}\n",
    "print(s1.issubset(s2))#s1是不是s2的子集\n",
    "print(s2.issuperset(s1))#s2是不是s1的父集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "95f365dd-1258-468b-8b30-61747e3274ac",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 10}\n",
      "{1, 2, 10}\n",
      "{1, 2, 10, 15, 50, 20, 22}\n",
      "{1, 2, 10, 15, 50, 20, 22}\n",
      "{50, 20}\n",
      "{22, 15}\n",
      "{50, 20}\n",
      "{50, 20, 22, 15}\n"
     ]
    }
   ],
   "source": [
    "s1 = {1,2,10,20,50}\n",
    "s2 = {22,10,15,1,2}\n",
    "print(s1.intersection(s2)) #求交集\n",
    "print(s1 & s2) #求交集\n",
    "print(s1 | s2) #求并集\n",
    "print(s1.union(s2))\n",
    "print(s1-s2)\n",
    "print(s2-s1)#求差集\n",
    "print(s1.difference(s2))#相当于s1-s2\n",
    "print(s1.symmetric_difference(s2)) #对称差，两个集合求差集之后再求并集"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75ceae8c-2bca-45b5-8c4a-dfb1bfbb5a57",
   "metadata": {},
   "source": [
    "## 5、函数\n",
    "### 5.1、函数基础"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "76ca35bb-c205-47b8-bb4f-aa050a6587d0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "34\n"
     ]
    }
   ],
   "source": [
    "def add(a,b):\n",
    "    return a+b\n",
    "print(add(12,22))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "298aca5f-5a71-4624-b124-7af8ce9c09c9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10 20\n",
      "[2, 1]\n",
      "[2, 1, 12]\n"
     ]
    }
   ],
   "source": [
    "def swap(a,b):\n",
    "    a,b = b,a\n",
    "x1 = 10\n",
    "x2 = 20\n",
    "swap(x1,x2)\n",
    "print(x1,x2)\n",
    "def swap(a):\n",
    "    a[0],a[1] = a[1],a[0]\n",
    "lst = [1,2]\n",
    "swap(lst)\n",
    "print(lst)\n",
    "def f(a):\n",
    "    a.append(12)\n",
    "f(lst) #lst是个可变元素，传递了指针中的值过去，所以元素会变化\n",
    "print(lst)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c2e8d61d-188d-48ba-8b3a-7a33a8df70a2",
   "metadata": {},
   "source": [
    "### 5.2、函数参数的默认值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "e40d4b08-febd-4945-8cd0-573f4307c6c7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "28"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#c的默认值100\n",
    "def fn(a,b=10,c=100):\n",
    "    return a+b+c\n",
    "print(fn(1,2,3))\n",
    "#有默认值的参数只能放在最后面\n",
    "# def fn1(a=10,b,c):#会报错，有默认值的参数必须放在最后面\n",
    "def fn1(b,c,a=10):\n",
    "    return a+b+c\n",
    "fn1(12,13,3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c3b5d873-0fb5-483b-b5db-2ce0bf343158",
   "metadata": {},
   "source": [
    "### 5.3、函数的列表和字典参数\n",
    "#### 5.3.1、函数的列表参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "bfc4b84c-fa07-4d5c-9694-f8ca45d7f9a3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1926604642000 <class 'tuple'> (1, 2, 3, 4)\n",
      "1926605829952 <class 'tuple'> (1, 2, 34)\n",
      "37\n"
     ]
    }
   ],
   "source": [
    "def fn1(*arg):\n",
    "    print(id(arg),type(arg),arg)\n",
    "    sum = 0\n",
    "    for i in arg:\n",
    "        sum+=i\n",
    "    return sum\n",
    "r = fn1(1,2,3,4)\n",
    "r\n",
    "#相当于将[1,2,3,4]作为一个参数传递给函数,而列表是没有办法做加法的\n",
    "# r = fn1([1,2,3,4])\n",
    "lst = [1,2,34]\n",
    "r = fn1(*lst) #把列表解封为单独的参数\n",
    "print(r)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2492b4e0-3450-4891-86f6-cf97b57baef5",
   "metadata": {},
   "source": [
    "### 5.3.2、字典参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "fd2b5fef-a9dd-4ef2-81ca-e24f37424fe8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'> {'a': 10, 'b': 20, 'c': 30}\n",
      "<class 'dict'> {'name': '张三', 'age': 22}\n"
     ]
    }
   ],
   "source": [
    "def fn2(**arg):\n",
    "    print(type(arg),arg)\n",
    "fn2(a=10,b=20,c=30)\n",
    "d = {\"name\":\"张三\",\"age\":22}\n",
    "#如果要传字典，也要进行解封装\n",
    "fn2(**d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "f7602a3f-4054-4811-a6a2-5368473d7f2c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 (1, 2, 3) {'a1': 10, 'b2': 2}\n"
     ]
    }
   ],
   "source": [
    "#列表在前，字典在后，不能换\n",
    "def fn3(a,b,*a1,**a2):\n",
    "    print(a,b,a1,a2)\n",
    "\n",
    "fn3(1,2,*[1,2,3],**{'a1':10,'b2':2})   "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e018d42-83a7-4503-abff-6ecacfc0afec",
   "metadata": {},
   "source": [
    "## 6、雇员管理系统第一版\n",
    "用字典来存储雇员信息{\"eno\":'123','name':'张三','salary':1000,'mobile':'123123123'}\n",
    "所有的雇员对象存储在lst中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "76eca333-75c6-42a4-99dd-653eb0a839d6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请选择一个具体的操作:添加(A)|删除(D)|更新(U)|列表(L)|退出(Q)\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      " A\n",
      "请输入员工编号: 001\n",
      "请输入员工姓名: 张三\n",
      "请输入薪水: 1000\n",
      "请输入手机号: 19332323223\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "---------------------------------------------------->添加员工成功<----------------------------------------------------\n",
      "请选择一个具体的操作:添加(A)|删除(D)|更新(U)|列表(L)|退出(Q)\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      " L\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "编号        姓名        薪水        联系电话           \n",
      "001         张三        1000       19332323223    \n",
      "请选择一个具体的操作:添加(A)|删除(D)|更新(U)|列表(L)|退出(Q)\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      " U\n",
      "请输入员工编号: 001\n",
      "请输入更新的姓名(张三): 李四\n",
      "请输入更新的薪水(1000): \n",
      "请输入更新的电话(19332323223): 123123123\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "---------------------------------------------------->更新员工成功!<----------------------------------------------------\n",
      "请选择一个具体的操作:添加(A)|删除(D)|更新(U)|列表(L)|退出(Q)\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      " L\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "编号        姓名        薪水        联系电话           \n",
      "001         李四        1000       123123123      \n",
      "请选择一个具体的操作:添加(A)|删除(D)|更新(U)|列表(L)|退出(Q)\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      " D\n",
      "请输入删除的员工号: 001\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "---------------------------------------------------->删除员工成功！<----------------------------------------------------\n",
      "请选择一个具体的操作:添加(A)|删除(D)|更新(U)|列表(L)|退出(Q)\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      " L\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "编号        姓名        薪水        联系电话           \n",
      "请选择一个具体的操作:添加(A)|删除(D)|更新(U)|列表(L)|退出(Q)\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      " Q\n"
     ]
    }
   ],
   "source": [
    "emps = []\n",
    "#emp ={\"eno\":'123','name':'张三','salary':1000,'mobile':'123123123'} \n",
    "\n",
    "def show_operator():\n",
    "    print(\"请选择一个具体的操作:添加(A)|删除(D)|更新(U)|列表(L)|退出(Q)\")\n",
    "\n",
    "def show_title():\n",
    "    format_title = '{:<10}{:<10}{:<10}{:<15}'\n",
    "    print(format_title.format('编号','姓名','薪水','联系电话'))\n",
    "\n",
    "def show_data(emp):\n",
    "    format_data = '{:<12}{:<10}{:<11}{:<15}'\n",
    "    print(format_data.format(emp['eno'],emp['name'],emp['salary'],emp['mobile']))\n",
    "\n",
    "def get_emp(no):\n",
    "    for emp in emps:\n",
    "        if emp['eno']==no:\n",
    "            return emp #返回的是一个字典对象\n",
    "    return None\n",
    "\n",
    "def error_msg(msg):\n",
    "    print(\"*********************************************\",end='')\n",
    "    print(f'{msg}',end='')\n",
    "    print(\"*********************************************\")\n",
    "\n",
    "def suc_msg(msg):\n",
    "    print(\"---------------------------------------------------->\",end='')\n",
    "    print(f\"{msg}\",end='')\n",
    "    print(\"<----------------------------------------------------\")\n",
    "\n",
    "def add_emp():\n",
    "    no = input(\"请输入员工编号:\")\n",
    "    emp = get_emp(no)\n",
    "    # print(not emp)\n",
    "    if emp!=None:\n",
    "        error_msg('添加的员工已经存在，不能添加!')\n",
    "        return\n",
    "    name = input(\"请输入员工姓名:\")\n",
    "    salary = int(input(\"请输入薪水:\"))\n",
    "    mobile = input(\"请输入手机号:\")\n",
    "    emp = {}\n",
    "    emp['eno'] = no\n",
    "    emp['name'] = name\n",
    "    emp['salary'] = salary\n",
    "    emp['mobile'] = mobile\n",
    "    emps.append(emp)\n",
    "    suc_msg(\"添加员工成功\")\n",
    "\n",
    "def list_emp():\n",
    "    show_title()\n",
    "    for emp in emps:\n",
    "        show_data(emp)\n",
    "\n",
    "def del_emp():\n",
    "    no = input(\"请输入删除的员工号:\")\n",
    "    emp = get_emp(no)\n",
    "    if emp==None:\n",
    "        error_msg(\"删除的员工不存在!\")\n",
    "        return\n",
    "    emps.remove(emp)\n",
    "    suc_msg(\"删除员工成功！\")\n",
    "\n",
    "def update_emp():\n",
    "    no = input(\"请输入员工编号:\")\n",
    "    emp = get_emp(no)\n",
    "    if emp==None:\n",
    "        error_msg(\"更新的员工不存在!\")\n",
    "        return\n",
    "    name = input(f\"请输入更新的姓名({emp['name']}):\")\n",
    "    salary = input(f\"请输入更新的薪水({emp['salary']}):\")\n",
    "    mobile = input(f\"请输入更新的电话({emp['mobile']}):\")\n",
    "    if name!='':\n",
    "        emp['name'] = name\n",
    "    if salary!='':\n",
    "        emp['salary'] = int(salary)\n",
    "    if mobile!='':\n",
    "        emp['mobile'] = mobile\n",
    "    suc_msg(\"更新员工成功!\")\n",
    "\n",
    "def main():\n",
    "    while(True):\n",
    "        show_operator()\n",
    "        op = input()\n",
    "        if op=='Q':\n",
    "            break;\n",
    "        elif op=='A':\n",
    "            add_emp()\n",
    "        elif op=='L':\n",
    "            list_emp()\n",
    "        elif op=='D':\n",
    "            del_emp()\n",
    "        elif op=='U':\n",
    "            update_emp()\n",
    "        else:\n",
    "            error_msg(\"请选择正确的操作!\")\n",
    "emp ={\"eno\":'12311','name':'张三','salary':1000,'mobile':'123123123'} \n",
    "# show_title()\n",
    "# show_data(emp)\n",
    "main()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a83a07ac-86e7-4728-a434-b3710e5c3a77",
   "metadata": {},
   "source": [
    "## 7、字符串\n",
    "### 7.1、字符串的基本操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "bf7474c0-15f5-401b-8dde-dc461c1bffb5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1753516392112 1753516392112\n",
      "1753518877424\n",
      "1001\n",
      "1753516392112 1753516392112 1753522566448\n",
      "1753516392112\n"
     ]
    }
   ],
   "source": [
    "s1 = '1001'\n",
    "s2 = '1001'\n",
    "# s1[0] = '2' #字符串是个常量,不能修改里面的元素，执行+法之后，是新分类了一块内存\n",
    "print(id(s1),id(s2))\n",
    "s3 = '100'\n",
    "print(id(s3))\n",
    "s3+='1' #其实是新分配了一块内存\n",
    "print(s3)\n",
    "#s3和s1和s2是不一样的 \n",
    "print(id(s1),id(s2),id(s3))\n",
    "s4 = '1001'\n",
    "print(id(s4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "108c3d49-4362-4fcb-bf83-86d0373fe27a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abc bcd\n"
     ]
    }
   ],
   "source": [
    "def swap(a,b):\n",
    "    a,b = b,a\n",
    "s1 = 'abc'\n",
    "s2 = 'bcd'\n",
    "swap(s1,s2)#字符串是不可变类型，类似于int这些，所以不会交换\n",
    "print(s1,s2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "985de49f-62cd-4cd8-9fd8-460196aa47de",
   "metadata": {},
   "source": [
    "### 7.2、字符串的常用操作\n",
    "字符串的操作和元组基本类似"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "98172645-f75d-417d-8afc-d9112d62e712",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "h 1753487614384\n",
      "h\n",
      "e\n",
      "l\n",
      "l\n",
      "o\n",
      "hello 1753487614384\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "s1 = 'hello'\n",
    "print(len(s1))\n",
    "print(s1[0],id(s1))\n",
    "for s in s1:\n",
    "    print(s)\n",
    "s1+=''\n",
    "print(s1,id(s1))\n",
    "print('he' in s1)#判断he是否在字符中"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77ec7a65-edde-4e13-9694-f5edbcbf2420",
   "metadata": {},
   "source": [
    "### 7.3、字符串的常用方法\n",
    "#### 7.3.1、字符串的查找"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "c83b605b-a9f7-47cb-96d0-db54ead3b15c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "-1\n",
      "2\n",
      "9\n",
      "java\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "s = 'hello world'\n",
    "print(s.index('ll')) #找子串的位置\n",
    "# print(s.index('aa')) #如果没有找到会抛出异常\n",
    "print(s.find('aa')) #如果没有找到会返回-1\n",
    "print(s.find('l'))\n",
    "print(s.rfind('l'))\n",
    "file = 'hello.java'\n",
    "#获取文件类型\n",
    "index = file.rfind('.')\n",
    "print(file[index+1:])\n",
    "print(file.endswith('.java')) #是否以.java结束\n",
    "print(file.startswith('he2ll'))#是否以某个字符串开始"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e7c845d4-7004-4602-afe8-50a3a48f13ea",
   "metadata": {},
   "source": [
    "#### 7.3.2、字符串的大小写操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "43f2bff1-f56e-43bc-afae-249317e3e585",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "HELLO YES hello YES\n",
      "hello yes hello YES\n",
      "HELLO yes\n",
      "好好学习，天天向上\n"
     ]
    }
   ],
   "source": [
    "s1 = 'hello YES'\n",
    "#upper之后的结果是生成一个新的字符串，原来的字符串不会修改\n",
    "print(s1.upper(),s1)\n",
    "s2 = s1.lower() #转换为小写\n",
    "print(s2,s1)\n",
    "s3 = s1.swapcase() #大小写交换\n",
    "print(s3)\n",
    "s1 = \"good good study,day day up\"\n",
    "s1 = '好好学习，天天向上'\n",
    "s2 = s1.title() #把第一个字母改成大写\n",
    "print(s2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75a6ff79-488d-45e6-b7b9-1d99d4997f5f",
   "metadata": {},
   "source": [
    "#### 7.3.3、字符串的分割和替换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "40b20683-5b07-4fc9-a068-6f97e0fe2062",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['hello', 'world', 'yes', 'ok']\n",
      "['1', '2', '3', '4']\n",
      "['1', '2', '3', '4']\n",
      "['1', '2', '3,4']\n",
      "['d:/test/aa/bb/cc', 'Hello.py']\n",
      "hello,b,b,b,b,b,b hello,a,a,a,a,a,a\n"
     ]
    }
   ],
   "source": [
    "s3 = 'hello world yes ok'\n",
    "lst = s3.split()#默认就是空格分割\n",
    "print(lst)\n",
    "s4 = '1,2,3,4'\n",
    "print(s4.split(','))\n",
    "print(s4.split(sep=','))#也是可以分割的\n",
    "print(s4.split(',',maxsplit=2))#表示最多分割2次\n",
    "filepath = 'd:/test/aa/bb/cc/Hello.py'\n",
    "print(filepath.rsplit(\"/\",maxsplit=1)) #通过一行代码讲文件和文件路径都取出来\n",
    "s5 = 'hello,a,a,a,a,a,a'\n",
    "s6 = s5.replace('a','b')\n",
    "print(s6,s5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bcd79885-d5a2-4866-bed0-130005cdbc47",
   "metadata": {},
   "source": [
    "### 7.4、字符串的切片\n",
    "字符串的切片和列表的切片完全一样，切完都是生成一个新的字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "47f1fe95-132a-4635-9705-86a3492addcc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dlrow olleh\n",
      "lo le\n",
      "rwolh\n"
     ]
    }
   ],
   "source": [
    "s = 'hello world'\n",
    "print(s[::-1])\n",
    "print(s[1::2][::-1])\n",
    "print(s[-3::-2])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "10934408-c9f1-4d67-856a-74376a8dd7f4",
   "metadata": {},
   "source": [
    "### 7.5、字符串的判断"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "95453b89-8d2a-46f4-bd69-817cc02b200a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1. False\n",
      "2. True\n",
      "3. True\n",
      "True\n",
      "True\n",
      "False\n",
      "True\n",
      "True\n",
      "True\n",
      "a   a|\n",
      "   a   a|\n",
      "a   a   |\n"
     ]
    }
   ],
   "source": [
    "s = 'hello python'\n",
    "print('1.',s.isidentifier()) #是否是合法标识符 False\n",
    "print('2.','hello'.isidentifier())\n",
    "print('3.','张三_'.isidentifier())\n",
    "\n",
    "print('     '.isspace()) #是否是所有空白\n",
    "print('abcAA'.isalpha()) #是否全部是字符\n",
    "print('abc.'.isalpha()) #是否全部是字符\n",
    "\n",
    "print('123'.isdecimal())#是否是十进制的数字组成\n",
    "\n",
    "print('一二壹'.isnumeric())#是否是数字，True\n",
    "print('abc123一'.isalnum()) #是否是数字和字母\n",
    "s1= '   a   a   ';\n",
    "print(s1.strip()+\"|\") #去除前后的空格\n",
    "print(s1.rstrip()+\"|\") #去除右边的空格\n",
    "print(s1.lstrip()+\"|\") #去除左边的空格"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f7c5400d-967c-40ae-9721-7a8b67f1b972",
   "metadata": {},
   "source": [
    "### 7.5、字符串的比较"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "d6612741-246a-4238-9f90-1b0570d89fa2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n",
      "97 65\n",
      "True\n",
      "True\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "s1 = 'apple'\n",
    "s2 = 'app'\n",
    "print(s1>s2)\n",
    "s3 = 'b'\n",
    "print(s1>s3)\n",
    "print(ord('a'),ord('A'))\n",
    "a = \"10\"\n",
    "b = '10'\n",
    "c = '1'\n",
    "c+='0'\n",
    "print(a==b)\n",
    "print(a is b)\n",
    "print(a==c) #判断字符串是否相等\n",
    "print(a is c)#判断id是否相等"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e69c62c0-4975-4d12-8701-165ab5cfac74",
   "metadata": {},
   "source": [
    "### 7.6、格式化字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "3f82b1d9-c6e5-456c-8d92-7ad7a635b18f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我叫张三,我的年龄是20\n",
      "我叫        张三\n",
      "        99\n",
      "89.323\n",
      "     3.142\n",
      "  12.333  \n"
     ]
    }
   ],
   "source": [
    "name = '张三'\n",
    "age = 20\n",
    "print(\"我叫%s,我的年龄是%d\"%(name,age))\n",
    "print('我叫%10s'%(name))\n",
    "\n",
    "print('%10d'%99)\n",
    "print('%.3f'%89.323232)\n",
    "print('%10.3f'%3.1415926111)#占10位的长度，保留三个字符\n",
    "print('{:^10.3f}'.format(12.33333))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b522e712-ee7c-4064-91c8-e523b9ad832d",
   "metadata": {},
   "source": [
    "## 8、面向对象\n",
    "### 8.1、面向对象基础"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "63b0ca2d-0a7b-4176-94e2-10beb76d2dc9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "旺财:呜呜呜!\n",
      "3108185849760 <class '__main__.Dog'> 3108144589168 <class 'type'> 3108175653552 <class 'str'>\n",
      "3108185849760 3108185849760\n",
      "小强:呜呜呜!\n",
      "皮皮:旺旺旺!\n",
      "普通\n",
      "12\n"
     ]
    }
   ],
   "source": [
    "class Dog:\n",
    "    def __init__(self,name,age):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "\n",
    "    def bark(self):\n",
    "        # self.size =22\n",
    "        if self.age<2:\n",
    "            print(f\"{self.name}:叽叽叽!\")\n",
    "        elif self.age<11:\n",
    "            print(f\"{self.name}:旺旺旺!\")\n",
    "        else:\n",
    "            print(f\"{self.name}:呜呜呜!\")\n",
    "\n",
    "d = Dog('旺财',12)\n",
    "d.bark()\n",
    "print(id(d),type(d),id(Dog),type(Dog),id(d.name),type(d.name))\n",
    "d1 = d\n",
    "d1.name = '小强'\n",
    "print(id(d1),id(d))\n",
    "d.bark()\n",
    "d2 = Dog('皮皮',6)\n",
    "d2.bark()\n",
    "d2.type='普通'\n",
    "print(d2.type)\n",
    "d1.size=12\n",
    "# print(d1.type)\n",
    "print(d.size)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "bdfd3fe9-9374-4e21-b524-96c9a526b42c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3108144577840 <class 'type'>\n",
      "3108191861248 <class '__main__.Person'>\n",
      "李四,22\n",
      "李四,22\n",
      "['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'age', 'money', 'name', 'talk']\n",
      "['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'age', 'name', 'talk']\n",
      "['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'talk']\n",
      "3108191552832 <class 'method'>\n"
     ]
    }
   ],
   "source": [
    "class Person:\n",
    "    def __init__(self,name,age):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "    def talk(self):\n",
    "        print(f\"{self.name},{self.age}\")\n",
    "\n",
    "print(id(Person),type(Person))\n",
    "p1 = Person(\"张三\",22)\n",
    "print(id(p1),type(p1))\n",
    "p2 = p1\n",
    "p2.name = '李四'\n",
    "p1.talk()\n",
    "p2.talk()\n",
    "p3 = Person('王五',44)\n",
    "p1.money = 1000\n",
    "print(dir(p1))\n",
    "print(dir(p3))\n",
    "print(dir(Person))\n",
    "print(id(p1.talk),type(p1.talk))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "827058d7-98d6-4195-aa91-066915da0580",
   "metadata": {},
   "source": [
    "### 8.2、类属性和类方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "390d6446-f762-4dd3-83df-545b7162871d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'classroom', 'show_classroom', 'study']\n",
      "小明:001\n",
      "小张:002\n",
      "计算机 计算机\n",
      "大数据 计算机\n",
      "3108185717904 3108185717904\n",
      "3108180634096 3108180634096\n",
      "大数据 物理学\n",
      "大数据 汉语言文学 物理学\n",
      "物理学\n"
     ]
    }
   ],
   "source": [
    "class Student:\n",
    "    classroom = '计算机' #是类属性，不会存储在对象中\n",
    "    def __init__(self,name,no):\n",
    "        self.name = name\n",
    "        self.no = no\n",
    "    def study(self):\n",
    "        print(f\"{self.name}:{self.no}\")\n",
    "\n",
    "    @classmethod#声明为类方法，这时,cls就表示类，而不是对象\n",
    "    def show_classroom(cls):\n",
    "        print(cls.classroom)\n",
    "print(dir(Student))\n",
    "\n",
    "s1 = Student(\"小明\",\"001\")\n",
    "s2 = Student(\"小张\",'002')\n",
    "s1.study()\n",
    "s2.study()\n",
    "print(s1.classroom,s2.classroom)\n",
    "s1.classroom = '大数据' #此时为s1创建了一个新的属性\n",
    "print(s1.classroom,s2.classroom)\n",
    "print(id(Student.classroom),id(s2.classroom))\n",
    "Student.classroom = \"物理学\"\n",
    "print(id(Student.classroom),id(s2.classroom))#s2的classroom和Student.classroom指向同一块内存\n",
    "print(s1.classroom,s2.classroom)\n",
    "s2.classroom = '汉语言文学'\n",
    "print(s1.classroom,s2.classroom,Student.classroom)\n",
    "# print(dir(s1),dir(s2))\n",
    "#结论就是，调用类的方法一定不能使用对象来调用，一定要用类名调用\n",
    "s1.show_classroom()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c0de94f-47ee-4e6e-a39b-8edfcb49dc79",
   "metadata": {},
   "source": [
    "### 8.3、静态方法\n",
    "静态方法存在的意义是就是给方法归类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "66771b30-6373-4e59-a522-33a6f68fe1c0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "test1\n",
      "test2\n"
     ]
    }
   ],
   "source": [
    "class Test:\n",
    "    @staticmethod\n",
    "    def test1():\n",
    "        print(\"test1\")\n",
    "\n",
    "    @staticmethod\n",
    "    def test2():\n",
    "        print(\"test2\")\n",
    "\n",
    "Test.test1()\n",
    "Test.test2() "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c60aa22e-1239-4a26-abfd-ab5aaa5e74df",
   "metadata": {},
   "source": [
    "### 8.4、动态绑定属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "3a53dd2e-e020-4e40-a933-10c7b0574360",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "<class 'function'>\n",
      "test_b:10\n",
      "test_b:10\n"
     ]
    }
   ],
   "source": [
    "import types\n",
    "class A:\n",
    "    def __init__(self,a):\n",
    "        self.a = a\n",
    "    def test_a(self):\n",
    "        print(self.a)\n",
    "\n",
    "a = A(10)\n",
    "a.test_a()\n",
    "\n",
    "def test_b(self):\n",
    "    print(f'test_b:{self.a}')\n",
    "# test_b('str')\n",
    "a.tb = test_b #并没有动态绑定成功\n",
    "print(type(a.tb)) #此时test_a是method对象，而test_b是function\n",
    "a.tb(a) #需要传入self才能调用成功，此时test_b并不是成员方法，如果要变成成员方法，需要使用types.MethodType(test_b, a)\n",
    "\n",
    "a.tb1 = types.MethodType(test_b,a)#动态绑定成功!\n",
    "a.tb1()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f7e6a1ee-cb77-4c33-8c02-1b5f73c51972",
   "metadata": {},
   "source": [
    "### 8.5、继承"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "bec94792-1cd4-4727-86d7-d3c08b6d62de",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "旺财:22\n",
      "big\n"
     ]
    }
   ],
   "source": [
    "class Animal:\n",
    "    def __init__(self,name,age):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "    def show(self):\n",
    "        print(f'{self.name}:{self.age}')\n",
    "\n",
    "class Dog(Animal):\n",
    "    def __init__(self,name,age,size):\n",
    "        super().__init__(name,age)\n",
    "        self.size = size\n",
    "\n",
    "    def show(self):\n",
    "        super().show()\n",
    "        print(self.size)\n",
    "\n",
    "d = Dog('旺财',22,'big')\n",
    "d.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "c27a5849-f6a7-405a-a172-599da75b79cd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A--->a\n",
      "B----b\n",
      "['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'a', 'aa', 'b']\n",
      "A--->aa\n"
     ]
    }
   ],
   "source": [
    "class A:\n",
    "    def a(self):\n",
    "        print('A--->a')\n",
    "    def aa(self):\n",
    "        print('A--->aa')\n",
    "\n",
    "class B:\n",
    "    def b(self):\n",
    "        print('B----b')\n",
    "    def aa(self):\n",
    "        print('B--->aa')\n",
    "\n",
    "#C先继承A之后继承B\n",
    "#先继承先得值，会忽略后面继承的所有信息\n",
    "class C(A,B):\n",
    "    pass\n",
    "\n",
    "c = C()\n",
    "c.a()\n",
    "c.b()\n",
    "print(dir(c))\n",
    "c.aa()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "6b1f3432-9001-4270-b467-dd6f421e7ca1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']\n"
     ]
    }
   ],
   "source": [
    "#所有的对象都继承了object\n",
    "print(dir(object))\n",
    "#默认执行的是A继承于object\n",
    "class A(object):\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "eb4cd379-3bac-457a-a4e0-ac806529f2e6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "华硕 is run\n",
      "intel--->I7\n",
      "Nividia VideoCard\n",
      "memory:8GB\n",
      "win10  start-------\n",
      "****************************************************************************************************\n",
      "华硕 is run\n",
      "AMD--->毒龙999\n",
      "Nividia VideoCard\n",
      "memory:8GB\n",
      "win10  start-------\n"
     ]
    }
   ],
   "source": [
    "class CPU:\n",
    "    def __init__(self,name,band):\n",
    "        self.name = name\n",
    "        self.band = band\n",
    "\n",
    "    def run(self):\n",
    "        print(f\"{self.name}--->{self.band}\")\n",
    "\n",
    "class VideoCard:\n",
    "    def __init__(self,name,size):\n",
    "        self.name = name\n",
    "        self.size = size\n",
    "    def run(self):\n",
    "        print(f'{self.name} VideoCard')\n",
    "        print(f'memory:{self.size}GB')\n",
    "\n",
    "class MainBorad:\n",
    "    def __init__(self,name,cpu,video_card):\n",
    "        self.name = name\n",
    "        self.cpu = cpu\n",
    "        self.video_card = video_card\n",
    "    def startup(self):\n",
    "        print(f'{self.name} is run')\n",
    "        self.cpu.run()\n",
    "        self.video_card.run()\n",
    "        print('win10  start-------')\n",
    "\n",
    "c1 = CPU('intel','I7')\n",
    "v1 = VideoCard('Nividia',8)\n",
    "mb = MainBorad('华硕',c1,v1)\n",
    "mb.startup()\n",
    "print(\"*\"*100)\n",
    "c2 = CPU('AMD','毒龙999')\n",
    "mb.cpu = c2\n",
    "mb.startup()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc43bc85-e105-4e32-b721-c90e8f98096a",
   "metadata": {},
   "source": [
    "### 8.6、深拷贝和浅拷贝"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "c215ddaf-0a68-4449-9449-0359a04bd631",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "AMD is run\n",
      "intel--->I9\n",
      "Nividia VideoCard\n",
      "memory:8GB\n",
      "win10  start-------\n",
      "****************************************************************************************************\n",
      "华硕 is run\n",
      "intel--->I9\n",
      "Nividia VideoCard\n",
      "memory:8GB\n",
      "win10  start-------\n",
      "****************************************************************************************************\n",
      "DEll is run\n",
      "TEST--->I9\n",
      "Nividia VideoCard\n",
      "memory:8GB\n",
      "win10  start-------\n",
      "****************************************************************************************************\n",
      "华硕 is run\n",
      "intel--->I9\n",
      "Nividia VideoCard\n",
      "memory:8GB\n",
      "win10  start-------\n"
     ]
    }
   ],
   "source": [
    "class CPU:\n",
    "    def __init__(self,name,band):\n",
    "        self.name = name\n",
    "        self.band = band\n",
    "\n",
    "    def run(self):\n",
    "        print(f\"{self.name}--->{self.band}\")\n",
    "\n",
    "class VideoCard:\n",
    "    def __init__(self,name,size):\n",
    "        self.name = name\n",
    "        self.size = size\n",
    "    def run(self):\n",
    "        print(f'{self.name} VideoCard')\n",
    "        print(f'memory:{self.size}GB')\n",
    "\n",
    "class MainBorad:\n",
    "    def __init__(self,name,cpu,video_card):\n",
    "        self.name = name\n",
    "        self.cpu = cpu\n",
    "        self.video_card = video_card\n",
    "    def startup(self):\n",
    "        print(f'{self.name} is run')\n",
    "        self.cpu.run()\n",
    "        self.video_card.run()\n",
    "        print('win10  start-------')\n",
    "c1 = CPU('intel','I7')\n",
    "v1 = VideoCard('Nividia',8)\n",
    "mb = MainBorad('华硕',c1,v1)\n",
    "# mb.startup()\n",
    "# mb1 = mb #直接进行指针的赋值\n",
    "# mb1.name = 'intel'\n",
    "# mb.startup()\n",
    "import copy\n",
    "mb1 = copy.copy(mb) #浅拷贝\n",
    "mb1.name='AMD'\n",
    "#此时mb1的name换成了AMD,mb并没有换\n",
    "mb1.cpu.band = 'I9'\n",
    "mb1.startup()\n",
    "print(\"*\"*100)\n",
    "mb.startup()\n",
    "print(\"*\"*100)\n",
    "mb2 = copy.deepcopy(mb)\n",
    "mb2.name = 'DEll'\n",
    "mb2.cpu.name = 'TEST'\n",
    "mb2.startup()\n",
    "print(\"*\"*100)\n",
    "mb.startup()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "46444ab6-975e-4625-9374-329a600870ae",
   "metadata": {},
   "source": [
    "### 8.6、类的初始化顺序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "89afd76c-2dbb-46fd-9620-a9fd3361a859",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "new方法被调用，cls的id为1792751300304\n",
      "obj被创建:id为1792797067728\n",
      "init被调用id为:1792797067728\n",
      "****************************************************************************************************\n",
      "Dog:1792751300304\n",
      "1792797067728 <class '__main__.Dog'>\n",
      "new方法被调用，cls的id为1792751300304\n",
      "obj被创建:id为1792799551056\n",
      "init被调用id为:1792799551056\n"
     ]
    }
   ],
   "source": [
    "#所有类都继承于object\n",
    "class Dog:\n",
    "    def __init__(self,name):\n",
    "        print(f\"init被调用id为:{id(self)}\")\n",
    "        self.name = name\n",
    "        \n",
    "    def __new__(cls,*args,**kwargs):\n",
    "        print(f\"new方法被调用，cls的id为{id(cls)}\")\n",
    "        #super()的父类是object\n",
    "        obj = super().__new__(cls)\n",
    "        print(f\"obj被创建:id为{id(obj)}\")\n",
    "        #返回对象\n",
    "        return obj\n",
    "        \n",
    "d = Dog(\"旺财\")\n",
    "print(\"*\"*100)\n",
    "print(f\"Dog:{id(Dog)}\")\n",
    "print(id(d),type(d))\n",
    "d2 = Dog(\"小强\")\n",
    "d3 = d #没有调用__new__因为只是指针指向d的内存"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fbd09a1d-05b4-4c93-962e-50f43261b2d0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
